﻿//* Copyright(C) 2005-2012, all rights reserved.
//*
//* The following code is copyrighted and contains proprietary information
/* and trade secrets of Foxit Software Inc.      
**************************************************************************
*/

/**
* 导航插件类；
*/



define(function (require, exports, module) {
    var CNavigationPanel_MainFrmEventHandler = require('./NPMainFrmEventHandler');
    FXREADER.CNavigationPanelPlugin = function (pReaderApp) {
        //子窗口列表；需要实现INavigationChildPanel接口;
        var _ChildPanelList = [],
        //当前处于显示状态的Panel;
              _CurVisableChildPanel = null,
              _pReaderApp = pReaderApp,
        _self = this,
        //按钮面板ID
        _tabPanelId = "tabPanel",
        //存放按钮的ul列表;
              _ButtonListID = "btnList",
        //点击之后展开面板的ID;
              _popleftId = "leftpopimg",
        //右侧面板ID，存放子面板内容;
              _TreePanelID = "fxtree_panel",
        //标题面板；
              _titlePanelId = "biaotou",
        //标题ID;
              _titleID = "panelTitle",
        //隐藏面板的按钮ID;
              _backbtId = "lefthid",
              _TreeSplitterID = "splitter_navigation_panel",
              _TreeMoveSplitterID = "splitter_navigation_panel__move",
        //右侧面板是否处于显示状态;
               _bContentVisable = FXREADER.Config.leftPanelShowOnStart;
        	var _defaultPanelWidth = 242;
        //创建面板的div内容；
        var _panelSize =
        {
            nTreePanelWidth: 0,
            nTreePanelHeight: 0
        };
        function _CreateHtmlContent() {
        	  //左侧的按钮面板；
            var html = "<div id='" + _tabPanelId + "' class='fwr_navigation_bar_tabmenu' style='height:100%'>" +
            		"<ul id='" + _ButtonListID + "'>";
            html += "<li id='popleft' class='fwr_left_panel fwr_left_panel_border_bottom' style='height:24px;width:40px'><div id='"
                            + _popleftId + "' class='fwr_hidden fwr_expand_panel_btn'></div></li></ul></div>";

            //右侧面板；
            html += "<div id='" + _TreePanelID + "' class='fwr_hidden fwr_left_panel_content' style='position:absolute;overflow: hidden; width: 180px; height: 100%;  color:#456176;'>";
            //右侧标题面板;
            html += "<div class='fwr_left_panel fwr_left_panel_border_bottom ' id='" + _titlePanelId + "' style='color:#171717; font-size:14px; height:24px;width:100%'>";

            html += "<div style='text-align: left;font-size: 13px;'><div style='float:left;' id='" + _titleID + "'></div><div id='" + _backbtId
                            + "' style='float:right;' class='fwr_hide_panel_btn'></div></div>";
            //end 右侧标题面板;
            html += "</div>";
            //end 右侧面板;
            html += "</div>";

            //拖动层；
            html += "<div id='" + _TreeSplitterID + "' style='height:100%' class='fwr_hidden fwr_windows_splitter' title='Click and drag to resize.'>" +
                            "</div>";
            //隐藏的拖动层；
            html += "<div id='" + _TreeMoveSplitterID + "' style='height:100%;position:absolute;left:-10000px;' class='fwr_windows_splitter fwr_top_layer' title='Click and drag to resize.'>" +
                            "</div>";
            //<img width='5px' height='auto' src='images/splitter_line.png'>
            $("#" + _pReaderApp.GetMainView().GetLeftPanelID()).append(html);
        }



        function _BindEvents() {
            $("#" + _backbtId).bind("click", function () { _self.DisablePanel(true); _self.OnSize(); });
            $("#" + _popleftId).bind("click", function () {
                if(!_self.IsVisablePanel())
                {	
                	if(_CurVisableChildPanel == null)
                		_self.ShowChildPanel(_ChildPanelList[0]);
                	else
                		_self.ShowChildPanel(_CurVisableChildPanel);
                	var jqLeftPanel = $("#" + _pReaderApp.GetMainView().GetLeftPanelID());
                	if(jqLeftPanel.width() < _defaultPanelWidth)
                	{
                		 var jqTreePanel = $("#" + _self.GetTreePanelDivID());
                		 jqTreePanel.width(170);
                	}
            		_self.DisablePanel(false); 
                }
                _self.OnSize(); return false;
            });

            var iCnt = _ChildPanelList.length;
            for (var i = 0; i < iCnt; i++) {
                var panel = _ChildPanelList[i];
                $("#" + panel.GetTabBtnID()).bind('click', { panel: panel }, function (event) {
                    _self.ShowChildPanel(event.data.panel); return true;
                });
            }
        }

        //public methods

        this.GetSplitterDivID = function () {
            return _TreeSplitterID;
        };

        this.GetMoveSplitterDivID = function () {
            return _TreeMoveSplitterID;
        };
        this.GetTreePanelDivID = function () {
            return _TreePanelID;
        };

        this.GetTabPanelDivID = function () {
            return _tabPanelId;
        };

        this.GetPanelWidth = function () {
            var jqTabPanel = $("#" + _tabPanelId);
            var jqTreePanel = $("#" + _TreePanelID);
            var jqTreeSplitter = $("#" + _TreeSplitterID);
            var nTabPanelWidth = jqTabPanel.is(":hidden") ? 0 : jqTabPanel.width();
            var nTreePanelHeight = jqTreePanel.is(":hidden") ? 0 : jqTreePanel.width();
            var nTreeSplitterWidth = jqTreeSplitter.is(":hidden") ? 0 : jqTreeSplitter.innerWidth();
            return nTabPanelWidth + nTreePanelHeight + nTreeSplitterWidth;
        };


        //添加子窗口；
        this.AddChildPanel = function (pChildPanel) {
            if (pChildPanel == null)
                return;
            _ChildPanelList.push(pChildPanel);
        };

        this.IsVisablePanel = function () { return _bContentVisable; };

        this.DisablePanel = function (bDis) {
            if (bDis) {
                $("#" + _TreePanelID).addClass("fwr_hidden");
                $("#" + _TreeSplitterID).addClass("fwr_hidden");
                $("#" + _popleftId).removeClass("fwr_hidden");
				if (_CurVisableChildPanel != null) {
					$("#" + _CurVisableChildPanel.GetPanelID()).addClass("fwr_hidden");
					$("#" + _CurVisableChildPanel.GetTabBtnID() + ">img").attr("src", _CurVisableChildPanel.GetTabNorImgUrl());
				}
            }
            else {
                $("#" + _TreePanelID).removeClass("fwr_hidden");
                $("#" + _TreeSplitterID).removeClass("fwr_hidden");
                $("#" + _popleftId).addClass("fwr_hidden");
            }
            _bContentVisable=!bDis;
        };

        //显示某个子panel;
        this.ShowChildPanel = function (pChildPanel2Show) {
            if (!_bContentVisable) {
                _self.DisablePanel(false); _self.OnSize();
            }

            if (pChildPanel2Show == null)
                return;
            //if (_CurVisableChildPanel == pChildPanel2Show)
            //   return;
            if (_CurVisableChildPanel != null) {
                $("#" + _CurVisableChildPanel.GetPanelID()).addClass("fwr_hidden");
                $("#" + _CurVisableChildPanel.GetTabBtnID() + ">img").attr("src", _CurVisableChildPanel.GetTabNorImgUrl());
                $("#" + _CurVisableChildPanel.GetTabBtnID()).removeClass("fwr_tab_selected");
            }

            var jqPanel = $("#" + pChildPanel2Show.GetPanelID());
            if (jqPanel.hasClass("fwr_hidden")) {
                jqPanel.removeClass("fwr_hidden");
                pChildPanel2Show.OnSize(_panelSize.nTreePanelWidth, _panelSize.nTreePanelHeight);
                $("#" + _titleID).text(pChildPanel2Show.GetTitle());
                $("#" + pChildPanel2Show.GetTabBtnID() + ">img").attr("src", pChildPanel2Show.GetTabHotImgUrl());
                $("#" + pChildPanel2Show.GetTabBtnID()).addClass("fwr_tab_selected");
            }
            _CurVisableChildPanel = pChildPanel2Show;
        };


        this.OnSize = function () {
            var jqLeftPanel = $("#" + _pReaderApp.GetMainView().GetLeftPanelID());
            jqLeftPanel.width(_self.GetPanelWidth());
            _pReaderApp.OnResize();
        };

        this.OnMainFrmSize = function () {
            if (!_bContentVisable)
                return;
            var jqTreePanel = $("#" + _TreePanelID);

            var jqLeftPanel = $("#" + _pReaderApp.GetMainView().GetLeftPanelID());
            var nLeftPanelHeight = jqLeftPanel.height();
            var nTabPanelWidth = $("#" + _tabPanelId).width();
            //这一句主要是为了兼容ie8，jqTreePanel高度设为100%获取的高度值确是错的;
            jqTreePanel.css({ left: nTabPanelWidth, height: nLeftPanelHeight });
            $("#" + _TreeSplitterID).css({ left: nTabPanelWidth + jqTreePanel.width(), height: nLeftPanelHeight });
            $("#" + _TreeMoveSplitterID).height(nLeftPanelHeight);
            var nTreePanelWidth = jqTreePanel.width();
            var nTreePanelHeight = jqTreePanel.height() - $("#" + _titlePanelId).height();
            _panelSize.nTreePanelWidth = nTreePanelWidth;
            _panelSize.nTreePanelHeight = nTreePanelHeight;
            var iCnt = _ChildPanelList.length;
            if (_CurVisableChildPanel != null)
                _CurVisableChildPanel.OnSize(nTreePanelWidth, nTreePanelHeight);
        };

        //获取Plugin名称;
        this.GetPluginName = function () {
            return "NavigationPanel Plugin";
        };
        //插件被注册的时候调用;
        this.PIOnRegister = function () {
        };
        //模块初始化;
        this.PIInit = function () {
            _CreateHtmlContent();
            var iCnt = _ChildPanelList.length;
            var jqBtnList = $("#" + _ButtonListID);
            var jqTreePanel = $("#" + _TreePanelID);

            for (var i = 0; i < iCnt; i++) {
                var panel = _ChildPanelList[i];
                if (i == iCnt - 1) {
                    jqBtnList.append("<li class='fwr_left_panel fwr_left_panel_border_bottom fwr_left_panel_border_right' id='" + panel.GetTabBtnID() + "' style='cursor:pointer;' title='" +
                        panel.GetTitle() + "'><img src='" + panel.GetTabNorImgUrl() + "' style='width:24px; height:24px;margin-top:8px;' /></li>");
                }
                else {
                    jqBtnList.append("<li class='fwr_left_panel fwr_left_panel_border_bottom fwr_left_panel_border_right' id='" + panel.GetTabBtnID() + "' style='cursor:pointer;' title='" +
                        panel.GetTitle() + "'><img src='" + panel.GetTabNorImgUrl() + "' style='width:24px; height:24px;margin-top:8px;' /></li>");
                }
                jqTreePanel.append("<div  id='" + panel.GetPanelID() + "' class='fwr_hidden jScrollPaneContainer jScrollPaneScrollable '></div>");
            }

            if (_bContentVisable) {
                _self.DisablePanel(false);
            }
            else {
                _self.DisablePanel(true);
            }

            _BindEvents();
            var pMainFrmEvnetHandler = new CNavigationPanel_MainFrmEventHandler(_self);
            _pReaderApp.RegisterMainFrmEventHandler(pMainFrmEvnetHandler);
            _self.OnSize();
            if (iCnt > 0 && _bContentVisable)
                _self.ShowChildPanel(_ChildPanelList[0]);
            _pReaderApp.StepProgress();
        };

        //模块卸载;
        this.PIUnload = function () {
        };

        this.GetReaderApp = function () {
            return _pReaderApp;
        };
    };



    exports.Reader_CreatePlugin = function (pReaderApp) {
        var pNavigationPanelPlugin = new FXREADER.CNavigationPanelPlugin(pReaderApp);
        return pNavigationPanelPlugin;
    };
});